<?php

class Polynomial 
{
    private array $__coeffs;
    private int $__deg;

    // Конструктор
    public function __construct(array $coeffs) {
        $this->__coeffs = $coeffs;
        $this->__deg = count($coeffs) - 1;
    }

    // Получение коэффициентов многочлена
    public function get_coeffs(): array {
        return $this->__coeffs;
    }

    // Получение степени многочлена
    public function get_deg(): int {
        return $this->__deg;
    } 

    // Преобразование многочлена в строку
    public function __toString(): string {
        $printable = "";
        foreach ($this->__coeffs as $index => $coeff) {
            if ($coeff == 0) continue;
            if ($index == 0 && $coeff < 0) $printable .= "-";
            if ($index > 0 && $coeff > 0) $printable .= " + ";
            if ($index > 0 && $coeff < 0) $printable .= " - ";
            if (abs($coeff) != 1) $printable .= abs($coeff);
            if ($this->__deg - $index > 1) $printable .= "x^".($this->__deg - $index);
            if ($this->__deg - $index == 1) $printable .= "x";
        }
        return rtrim($printable, " -");
    }
}